﻿using Ducu.SRV.ServiceInteface;
using Ducu.SRV.ViewModel.User;
using Ducu.UI.Responsible.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Global.Core.ExtensionMethod;
using Ducu.GLB.Global;
using Ducu.SRV.ViewModel.Shared;

namespace Ducu.UI.Responsible.Controllers
{
    public class UserController : BaseController
    {
        #region Constructor

        private IUserService _userService;
        private IMessageService _messageService;
        private ISharedService _shareService;
        public UserController(IUserService userService,
            IMessageService messageService,
            ISharedService shareService)
        {
            _userService = userService;
            _messageService = messageService;
            _shareService = shareService;
        }

        #endregion

        #region Common Actions

        #region URL: /User/Profile/{id}

        public new ActionResult Profile(int id)
        {
            ProfileModel model = _userService.Get(id);
            int currentUserId = _shareService.GetNavigator().UserId;
            model.HasLogon = currentUserId > 0;
            model.IsCurrentUser = currentUserId == id;
            ViewBag.PrepageUrl = Prepage.Get();
            return View(model);
        }

        [NeedLogOn]
        [HttpPost]
        [ValidateInput(false)]
        public new ActionResult Profile(ProfileModel model, int id)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            _messageService.SendFromMe(model.Message.HtmlEncode(), id);

            return RedirectToAction("Profile", new { id = id });
        }

        #endregion

        #region URL: /User/Credits

        public ActionResult Credits(int userId, int pageIndex = 1)
        {
            int pageSize = 10;
            Pager queryPager = new Pager(pageSize, pageIndex);

            CreditsModel model = _userService.GetCreditsList(userId, queryPager);

            int sumOfItems = _userService.GetCreditsCount(userId);
            model.Pager = new _PagerModel
            {
                PageIndex = pageIndex,
                RowSize = 5,
                SumOfPage = queryPager.GetSumOfPage(sumOfItems)
            };

            return View(model);
        }

        #endregion

        #endregion

        #region Child Actions

        public PartialViewResult _Credit(int userId)
        {
            _CreditModel model = _userService.GetCredit(userId);
            return PartialView(model);
        }

        #endregion
    }
}